<?php
namespace app\user\controller;
use think\Db;
class Login extends Common
{
    public function other()
    {
        $other_id = request()->request('uid');
        $headimg = (string)request()->request('headimg');
        $name = (string)request()->request('name');
        $msg['state'] = 0;
        $msg['data']['level'] = 0;
        $msg['data']['token'] = '';
        if(empty($other_id)){
            $msg['msg'] = '参数错误';
        }else{
            $other = Db::table('user')->where(['other_id'=>$other_id])->find();
            if(count($other) == 0){
                $msg['state'] = 1;
                $id = Db::table('user')->insertGetId(['other_id'=>$other_id,'headimg'=>$headimg,'name'=>$name,'type'=>0,'regtime'=>time(),'status'=>1]);
                Db::table('user')->where('id',$id)->update(['token'=>md5($id)]);
                $msg['data']['token'] = md5($id);
            }else{
                if($other['status'] == 1){
                    $msg['state'] = 1;
                    $id = $other['id'];
                    $msg['msg'] = '登录成功';
                    $msg['data']['level'] = $other['level'];
                    $msg['data']['token'] = md5($id);
                }else{
                    $msg['msg'] = '此用户已停用';
                }
            }
        }
        return json($msg);
    }

    public function get_info()
    {
        $token = request()->request('token');
        $data = Db::table('user')->field('id,headimg,name')->where('token',$token)->find();
        $order = Db::table('order')->where(['ispay'=>0,'uid'=>$data['id']])->select();
        unset($data['id']);
        $money = 0;
        foreach ($order as $k =>$v){
            $money += $v['price'];
        }
        $data['money'] = $money;
        $data = (object)$data;
        return json(['code'=>1,'data'=>$data,'msg'=>'获取成功']);
    }

    public function get_about()
    {
        $data = Db::table('settings')->where('id',1)->value('about');
        return '<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /><title>关于我们</title>'.$data;
    }

    public function get_ad()
    {
        $data = Db::table('settings')->field('image,time,status')->where('id',1)->find();
        return json($data);
    }
}